package com.sctvcloud.longchang.ui.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.beans.FCityState;
import com.sctvcloud.longchang.beans.TvItem;
import com.sctvcloud.longchang.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.longchang.ui.fragment.EncyclopediaNewsListFragment;
import com.sctvcloud.longchang.ui.fragment.LiveFragment;
import com.sctvcloud.longchang.ui.fragment.ProgramBackFragment;
import com.sctvcloud.longchang.ui.fragment.RadioFragment;
import com.sctvcloud.longchang.ui.fragment.TvFragment;

import java.util.ArrayList;
import java.util.List;

public class EncyclopediaPagerAdapter extends FragmentPagerAdapter {

    private Context context;
    public static final int TYPE_TV = 1;//电视台
    public static final int TYPE_VIDEO = 2;//电台
    public static final int TYPE_BACK = 3;//新闻回看

    private List<FCityState> dataList = new ArrayList<>();

    public EncyclopediaPagerAdapter(Context context, FragmentManager fm, List<FCityState> list) {
        super(fm);
        if (null != list && list.size() > 0) {
            dataList.clear();
            this.dataList.addAll(list);
        }
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = new EncyclopediaNewsListFragment();
        ((EncyclopediaNewsListFragment) fragment).setChannelUrl(dataList.get(position).getInstitutionUrl());
        fragment.setIndexInViewPager(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return dataList.get(position).getInstitutionName();
    }

//    public View getTabView(int position) {
//        View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
//        ImageView iv = (ImageView) view.findViewById(R.id.tab_img);
//        TextView tv = (TextView) view.findViewById(R.id.tab_text);
//        tv.setText(dataList.get(position).getTitle());
//        iv.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getNormalImg()));
//        return view;
//    }

    public void setData(List<FCityState> data) {
        if ((dataList == null || dataList.size() == 0) && (null != data && data.size() > 0)) {
            dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //如果注释这行，那么不管怎么切换，page都不会被销毁
//        super.destroyItem(container, position, object);
    }


}
